#include<iostream.h>

#include<TROOT.h>
#include<TFile.h>
#include<TTree.h>
#include<TMath.h>
#include<TCanvas.h>
#include<TH1F.h>
#include<TH2F.h>
#include<TF1.h>
#include<TStyle.h>
#include<TApplication.h>

#include<Global.h>

int main(int argc, char* argv[]){
  
  gStyle->SetOptFit(1);
  gStyle->SetOptStat(1110);
  gStyle->SetPalette(1);

  Int_t RunNumber = atoi(argv[1]);

  TApplication app("myapp", &argc, argv);

  Global* glob;
  glob = new Global(RunNumber);

  Float_t DigX[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t DigY[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t CogX[glob->TELNumberOfPlanes][glob->TELMaxHit];
  Float_t CogY[glob->TELNumberOfPlanes][glob->TELMaxHit];

  Char_t Path[200];
  sprintf(Path,"../../ntuple/tele/TELHits%u.root",RunNumber);

  TFile *trackfile;
  trackfile = new TFile(Path);
  trackfile->cd();

  TTree *Tracks = (TTree*)trackfile->Get("TELHits");

  Tracks->SetBranchAddress("DigX",&DigX[0][0]);
  Tracks->SetBranchAddress("DigY",&DigY[0][0]);
  Tracks->SetBranchAddress("CogX",&CogX[0][0]);
  Tracks->SetBranchAddress("CogY",&CogY[0][0]);

  //Cog-Dig
  TH1F* h11[2][glob->TELNumberOfPlanes];
  TH1F* h12[2][glob->TELNumberOfPlanes];

  TCanvas* canv11 = new TCanvas("c_Cog-Dig","",1200,800);
  canv11->SetFillColor(0);
  canv11->Divide(glob->TELNumberOfPlanes,2);

  Char_t TELRoot[100];

  for(Int_t n = 0; n < glob->TELNumberOfPlanes; n++){

    sprintf(TELRoot,"Cog-Dig_X_%u",n+1);
    h11[0][n] = new TH1F(TELRoot,"",50,-1.0*glob->TELStripSizeX[n],1.0*glob->TELStripSizeX[n]); 
    h11[0][n]->GetYaxis()->SetTitle("Number of events");
    h11[0][n]->GetYaxis()->SetTitleOffset(1.35);
    h11[0][n]->GetXaxis()->SetTitle("x_{cog} - x_{dig} [#mum]");
    h11[0][n]->GetXaxis()->SetTitleSize(0.05);
    h11[0][n]->GetXaxis()->SetTitleOffset(0.9);
    h11[0][n]->SetLineWidth(3);
    h11[0][n]->SetLineColor(4);

    sprintf(TELRoot,"Cog-Dig_Y_%u",n+1);
    h11[1][n] = new TH1F(TELRoot,"",50,-1.0*glob->TELStripSizeY[n],1.0*glob->TELStripSizeY[n]); 
    h11[1][n]->GetYaxis()->SetTitle("Number of events");
    h11[1][n]->GetYaxis()->SetTitleOffset(1.35);
    h11[1][n]->GetXaxis()->SetTitle("y_{cog} - y_{dig} [#mum]");
    h11[1][n]->GetXaxis()->SetTitleSize(0.05);
    h11[1][n]->GetXaxis()->SetTitleOffset(0.9);
    h11[1][n]->SetLineWidth(3);
    h11[1][n]->SetLineColor(4);

  }//n

  for(Int_t i = 0; i < Tracks->GetEntries(); i++){
    
    Tracks->GetEntry(i);

    for(Int_t n = 0; n < glob->TELNumberOfPlanes; n++){
      for(Int_t m = 0; m < glob->TELMaxHit; m++){

	if(DigX[n][m] > 0 && CogX[n][m] > 0) h11[0][n]->Fill(CogX[n][m] - DigX[n][m]);
	if(DigY[n][m] > 0 && CogY[n][m] > 0) h11[1][n]->Fill(CogY[n][m] - DigY[n][m]);

      }//m
    }//n

  }//i

  for(Int_t n = 0; n < glob->TELNumberOfPlanes; n++){
    
    canv11->cd(n+1);
    h11[0][n]->Draw();
    canv11->cd(n + 1  + glob->TELNumberOfPlanes);
    h11[1][n]->Draw();
    
  }

  TCanvas* canvPhD11 = new TCanvas("c_Cog-Dig11","",600,600);
  canvPhD11->SetFillColor(0);
  canvPhD11->cd();
  h11[0][0]->Draw();

  TCanvas* canvPhD12 = new TCanvas("c_Cog-Dig12","",600,600);
  canvPhD12->SetFillColor(0);
  canvPhD12->cd();
  h11[1][0]->Draw();

  app.Run(true);
  
}
